import React, { memo, useState } from "react";
import css from './css.module.scss'
import { Upload, Button, Space, message } from 'antd';
import { UploadOutlined } from '@ant-design/icons';
import RechTitle from '../RechTitle'
import { BASE_URL } from '../../services/config'
import { login } from '../../common/login'
import cookie from '../../common/cookie'
import { addAwardDeclare } from '../../services/home'
export default memo(function DownList(props) {
    const { awardId } = props
    const headers = {
        Authorization: cookie.readCookie('userCode') || ''
    }
    const [fileList, updateFileList] = useState([]);
    const handlChange = (e) => {
        const { response } = e.file
        if (e.file.response) {
            if (response.code !== 200) {
                message.error(response.content)
                updateFileList([])
                if (response.code === 10400) {
                    login()
                }
            } else {
                updateFileList(e.fileList.filter(file => !!file.status));
                message.success(response.content)
            }

        }

    }
    const onRemove = (e) => {
        updateFileList([]);
    }
    const handSub = (e) => {
        console.log(fileList)
        if (fileList.length !== 0 && fileList[0].response) {
            const { data } = fileList[0].response
            addAwardDeclare({ awardId, status: 0, spared1: data }).then(res => {
                message.success(res.data)
                updateFileList([]);
            })
        } else {
            message.info('请点击上传!')
        }
        // this.form.spared1 = this.fileList[0].response.data;
        // this.form.awardId = this.details.awardId;
    }
    return (
        <div className={css.upload_box}>
            <RechTitle title='附件上传'></RechTitle>
            <Space direction="vertical" style={{ width: '100%' }} size="large">
                <Upload
                    headers={headers}
                    fileList={fileList}
                    onRemove={e => onRemove(e)}
                    onChange={e => handlChange(e)}
                    action={BASE_URL + "/uploadDownload/api/imgUpload"}
                    listType="picture"
                    maxCount={1}
                >
                    <Button icon={<UploadOutlined />}>点击上传 (最大: 1)</Button>
                </Upload>

            </Space>
            <div className={css.hint}>
                温馨提示：文件大小不超过800M，如文件过大请将所有材料以电子邮件发送至kejijiang@ecbda.org。或拷入U盘并邮寄至建筑装饰行业科学技术奖办公室（北京市海淀区三里河路21号甘家口大厦910B）
            </div>
            <div style={{ textAlign: 'center' }}>
                <Button type="primary" onClick={e => handSub(e)}>
                    立即申报
                 </Button>
            </div>
        </div>
    )
})
